package com.feiyu.common.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.feiyu.common.entity.AddressEntity;
import com.feiyu.common.service.AddressService;
import com.feiyu.common.utils.PageUtils;
import com.feiyu.common.utils.R;


/**
 * @author wanghanbin
 * @email 1336891708@qq.com
 * @date 2021-04-03 12:36:05
 */
@RestController
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 列表
     */
    @PostMapping("/searchAddress")
    //@RequiresPermissions("product:address:list")
    public R searchAddress(@RequestBody Map<String, Object> params) {
        PageUtils page = addressService.queryPageByMyself(params);
        if(page.getList().size()>0){
            return R.ok("查询地址成功").put("data", page);
        }else {
            return R.error("查询地址失败").put("data", null);
        }
    }

    @PostMapping("/searchAddressById")
    //@RequiresPermissions("product:address:info")
    public R searchAddressById(@RequestBody AddressEntity addressEntity) {
        AddressEntity address = addressService.getById(addressEntity.getAddressId());
        if (address != null) {
            return R.ok("精确查找成功").put("data", address);
        } else {
            return R.error("精确查找失败").put("data", null);
        }
    }

    /**
     * 保存
     */
    @RequestMapping("/addAddress")
    //@RequiresPermissions("product:address:save")
    public R save(@RequestBody AddressEntity address) {
        addressService.save(address);
        return R.ok("添加成功").put("data",null);
    }

    /**
     * 修改
     */
    @RequestMapping("/updateAddress")
    //@RequiresPermissions("product:address:update")
    public R updateAddress(@RequestBody AddressEntity address) {
        addressService.updateById(address);
        return R.ok("修改成功").put("data",null);
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteAddress")
    //@RequiresPermissions("product:address:delete")
    public R deleteAddress(@RequestBody AddressEntity addressEntity) {
        addressService.removeById(addressEntity.getAddressId());
        return R.ok("删除成功").put("data",null);
    }

}
